package com.leetone.microservice.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    /*
     * @description 【使用Hystrix来实现断路器，并通过注解来指定回调方法】
     * @param id
     * @return java.lang.String
     * @exception
     * @author leetone 【leetone123@163.com】
     * @date 2019/3/20 22:41
     */
    @GetMapping("findOrdersByUser/{id}")
    @HystrixCommand(fallbackMethod = "fallbackInfo")
    public String findOrdersByUser(@PathVariable String id) {
        return restTemplate.getForObject("http://microservice-eureka-order/order/" + id, String.class);
    }

    public String fallbackInfo(@PathVariable String id) {
        return "服务暂不可用，请稍后再试！";
    }
}
